import json
import pprint
import random
from enum import Enum


# 账号生成 id：系统随机产生8位数字

user_dict = {}



def random_num(num):
    start = 10 ** num
    end = 10 ** (num + 1) - 1
    return random.randint(start, end)


class Address:
    def __init__(self, country, sheng, jie, men):
        self.country = country
        self.sheng = sheng
        self.jie = jie
        self.men = men

    def __str__(self):
        return f"{self.country}, {self.sheng}, {self.jie}, {self.men}"


class Result(Enum):
    SUCCESS = 1
    EXISTS = 2
    FULL = 3


class Bank_name:
    def __init__(self, bank_name):
        self.bank_name = "中国工商银行" + bank_name

    def __str__(self):
        return self.bank_name


# 用户
class User:

    def __init__(self, name, password, address, money):
        self.id = random_num(8)
        if isinstance(name, str):
            self.name = name


        if isinstance(password, int) and len(str(password)) >= 6:
            self.password = password
        else:
            raise ValueError("密码必须是至少6位的整数")
        self.address = Address(address.country, address.sheng, address.jie, address.men)
        if isinstance(money,int) and money >= 0:
            self.money= money
        else:
            raise ValueError("余额必须是大于等于0的整数")
        self.bank_name = Bank_name("昌平支行")

    def __str__(self):
        return f"账户：{self.id}\n用户名：{self.name}\n密码：{self.password}\n地址：{self.address}\n余额：{self.money}\n开户行：{self.bank_name}"
    def show(self):
        self.__str__()

def user_add(user):
    if len(user_dict) >= 2:
        return Result.FULL.value
    for id in user_dict.keys():
        if id == user.id:
            return Result.EXISTS.value
    user_dict[user.id] = user
    return Result.SUCCESS.value

#存钱
def cun_money(id, money):
    flag=False
    for i in user_dict.keys():
      if i==id:
          user_dict[i].money += money
          return True
    return False

#取钱
def qu_money(id,password,money):
    for i in user_dict.keys():
      if i==id:
          if user_dict[i].password==password:
                if user_dict[i].money>=money:
                    user_dict[i].money-=money
                    return 0
                else:return 3
          else:
              return 2

    return 1

#转账
def zhaun_money(cid, rid, cpwd, cmoney):
        re1 = qu_money(cid, cpwd, cmoney)
        if re1 == 0:
            if cun_money(rid, cmoney):
                return 0
        elif re1 == 1:
            return 1
        elif re1 == 2:
            return 2
        elif re1 == 3:
            return 3
        else:
            return -1

#查询账户
def select_user(id,pwd):
    flag=False
    for i in user_dict.keys():
        if i == id:
            flag=True
            if pwd == user_dict[i].password:
                return user_dict[i]
            else:
                return print("密码错误")
    if flag==False:
        return print("用户不存在")

def main():
    while True:
        print("***********************************")
        print("*            中国工商银行           *")
        print("*            账户管理系统           *")
        print("*              v1.0               *")
        print("***********************************")
        print("\n")
        print("*1.开户                            *")
        print("*2.存钱                            *")
        print("*3.取钱                            *")
        print("*4.转账                            *")
        print("*5.查询                            *")
        print("*6.Bye                            *")
        print("***********************************")
        chioce=int(input("请输入您的选择："))
        if chioce==1:
           user=User(input("请输入您的用户名："),
                     int(input("请输入您的密码（必须为6位）：")),
                     Address(input("请输入您的国家："), input("请输入您的省份："),
                     input("请输入您的城市："),
                     input("请输入您的街道：")),
                     int(input("请输入您的账户金额：")))
           print("您所输入的信息为：\n\n\n\n{}".format(user))
           print("开户结果:{}".format(user_add(user)))
        elif chioce==2:
            id=int(input("请输入您的账号："))
            money=int(input("请输入您的存钱金额："))
            cun_money(id,money)
            print("存钱成功！")
        elif chioce==3:
            id=int(input("请输入您的账号："))
            password=int(input("请输入您的密码："))
            money=int(input("请输入您的存钱金额："))
            print("取钱结果：{}".format(qu_money(id,password,money)))
        elif chioce==4:
            cid=int(input("请输入您的账号："))
            rid=int(input("请输入对方账号："))
            cpwd=int(input("请输入您的密码："))
            cmoney=int(input("请输入转账金额："))
            print("转账结果：{}".format(zhaun_money(cid,rid,cpwd,cmoney)))
        elif chioce==5:
            id=int(input("请输入您的账户："))
            pwd=int(input("请输入您的密码："))
            print("查询结果:{}".format(select_user(id,pwd)))
        elif chioce==6:
            print("Bye")
            break
        else:
            print("输入错误")


if __name__ == "__main__":
            main()
